package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Author：汤小洋
 * Date：2022-11-28 15:04
 * Description：<描述>
 */
@Controller
@RequestMapping("/file")
public class UploadController {

    @RequestMapping("/upload")
    public String upload(String name, @RequestParam CommonsMultipartFile avatar, HttpServletRequest req) throws IOException {
        System.out.println(name);

        System.out.println(avatar.getName());
        System.out.println(avatar.getOriginalFilename());
        System.out.println(avatar.getSize());
        // System.out.println(avatar.getInputStream());

        String uploadPath = req.getServletContext().getRealPath("upload");
        String filename = StringUtil.renameFilename(avatar.getOriginalFilename());
        String filePath = uploadPath + File.separator + filename;
        System.out.println(filePath);
        avatar.transferTo(new File(filePath)); // 保存到磁盘上

        return "result";
    }
}
